<template>
	<view class="conter">
		<!-- 顶部栏目 -->
		<view class="top-text">
			<view class="top-text-icon">
				<u-icon name="checkmark" color="#FFFFFF" size="13"></u-icon>
				30天无忧退货
			</view>

			<view class="top-text-icon">
				<u-icon name="checkmark" color="#FFFFFF" size="13"></u-icon>
				48小时快速退费
			</view>

			<view class="top-text-icon">
				<u-icon name="checkmark" color="#FFFFFF" size="13"></u-icon>
				满88元免邮费
			</view>
		</view>
		<!-- 购物车为空的图案 -->
		<view class="beijing" v-show="cartTotal.goodsCount == 0 ? true : false">
			<image class="beijing-img" src="http://www.52coding.cc/net163/img/custom-empty-image.276c05f0.png" mode="widthFix"></image>
			<view class="beijing-text">购物车为空</view>
		</view>

		<!-- 购物车的商品 -->
		
		<view class="commodity" v-for="(item, index) in cartList" :key="item.product_id">
			<view class="commodity-group">
				<u-checkbox-group @change="checkedFn(item)">
					<u-checkbox :disabled="isShow == false ? true : false" size="20px" :checked="item.checked == 1" shape="circle" :name="item.product_id"></u-checkbox>
				</u-checkbox-group>
			</view>

			<u-swipe-action>
				<u-swipe-action-item @click="fn(item.product_id)" :options="options1">
					<view class="commodity-goods">
						<image :src="item.list_pic_url" mode="widthFix"></image>
						<view class="commodity-goods-text">
							<view>{{ item.goods_name }}</view>
							<view>
								<text>￥{{ item.retail_price }}.00</text>
								<text>x{{ item.number }}</text>
							</view>
							<u-number-box v-show="!isShow" v-model="item.number" @change="valChange(item)"></u-number-box>
						</view>
					</view>
				</u-swipe-action-item>
			</u-swipe-action>
		</view>
		<!-- 底部编辑 -->
		<view class="bottom-cart">
			<view class="bottom-cart-edit">
				累计共{{ cartTotal.checkedGoodsCount }}件商品，可点击
				<u-button type="primary" color="#00aa00" v-show="isShow" @tap="isShow = false" text="编辑"></u-button>
				<u-button type="primary" color="#ff0000" v-show="!isShow" @tap="isShow = true" text="完成编辑"></u-button>
				按钮进行商品数量修改
			</view>
			<view class="bottom-cart-purchase">
				<view class="bottom-cart-purchase1">
					<u-checkbox-group><u-checkbox :disabled="isShow == false ? true : false" :checked="checkAll" @change="checkAllFn" size="20px" shape="circle"></u-checkbox></u-checkbox-group>
					全选
				</view>
				<view class="bottom-cart-purchase2">合计 : ￥{{ cartTotal.checkedGoodsAmount }}.00</view>
				<view class="bottom-cart-purchase3"><u-button shape="circle" type="primary" color="#ff0000" text="立即购买"></u-button></view>
			</view>
		</view>
		<customTabbar></customTabbar>
	</view>
</template>

<script>
import customTabbar from '@/custom-tab-bar/index.vue';
import { ApiGetCart, ApiGetUpdate, ApiGetChecked } from '@/utils/api.js';

import { ApiGetDelete } from '@/utils/api.js';
export default {
	components: {
		customTabbar
	},
	data() {
		return {
	
			options1: [
				{
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}
			],
			value: 0,
			// 商品
			cartList: [],
			cartTotal: {},
			// 步进器和背景图的显示和隐藏
			isShow: true,
			checkAll: false,
		};
	},
	methods: {
		// 步进器
	  async	valChange(item) {
			let res = await ApiGetUpdate({
				goodsId:item.goods_id,
				id:item.id,
				number:item.number+1,
				productId:item.product_id
			})
		},
		// 删除商品
		async fn(i) {
			let id = i +''
			let res1 = await ApiGetDelete({
				productIds: id
			});
			let token = uni.getStorageSync('token');
			let res = await ApiGetCart({
				'X-Nideshop-Token': token
			});
			this.cartList = res.data.cartList;
			this.cartTotal = res.data.cartTotal;
		},
		// 全选
		async checkedFn(item) {
					let res = await ApiGetChecked({
						isChecked: item.checked === 1 ? 0 : 1,
						productIds: item.product_id + ''
					});
		
					let { cartList, cartTotal } = res.data;
					
					// 全选状态
					this.checkAll = cartList.every(item => item.checked === 1);
					this.cartList = cartList;
					this.cartTotal = cartTotal;
			},
			
			async checkAllFn(e) {
				let productId = this.cartList.map(item => item.product_id);
				let res;
				if (this.checkAll) {
					// 取消全选
					res = await ApiGetChecked({
						isChecked: 0,
						productIds: productId.join(',')
					})
				} else {
					res = await ApiGetChecked({
						isChecked: 1,
						productIds: productId.join(',')
					})
				}
				
				if (res.errno !== 0) return;
				
				let { cartList, cartTotal } = res.data;
				
				// 全选状态
				this.checkAll = cartList.every(item => item.checked === 1);
				this.cartList = cartList;
				this.cartTotal = cartTotal;
}
	},
       onShow() {
		this.$store.commit('tabbarIndex', 3);
		
	},
	// 商品渲染
		async onLoad() {
		let token = uni.getStorageSync('token');
		let res = await ApiGetCart({
			'X-Nideshop-Token': token
		});
		this.cartList = res.data.cartList;
		this.cartTotal = res.data.cartTotal;
	
		// 全选
		var id =""
		        this.cartList.forEach(ele=>{
		            return id +=ele.product_id+','
		        })
		        var ids = id.substring(0,id.length-1)
		let res1 = await ApiGetChecked({
			isChecked:0,
			productIds:ids
		})
		this.checkAll = this.cartList.every(item => item.checked === 1);
	}
};
</script>

<style lang="scss" scoped>
page {
	background-color: #efefef;
}
// <!-- 顶部栏目 -->
.top-text {
	width: 750rpx;
	height: 60rpx;
	display: flex;
	justify-content: space-between;
	padding: 0 20rpx;
	box-sizing: border-box;
	font-size: 26rpx;
	line-height: 60rpx;

	.top-text-icon {
		display: flex;
		.u-icon {
			background-color: red;
			width: 26rpx;
			height: 26rpx;
			border-radius: 100%;
			margin-top: 17rpx;
			margin-right: 10rpx;
		}
	}
}
// <!-- 购物车为空的图案 -->
.beijing {
	height: 750rpx;
	position: fixed;
	left: 220rpx;
	top: 0;
	bottom: 0;
	margin: auto;
	text-align: center;
	.beijing-img {
		width: 320rpx;
		height: 320rpx;
	}
	.beijing-text {
		margin-top: 36rpx;
		color: #969799;
	}
}
// <!-- 购物车的商品 -->
.commodity {
	width: 750rpx;
	margin-top: 5rpx;
	background-color: white;
	display: flex;
	padding: 20rpx;
	box-sizing: border-box;
	.commodity-group {
		display: flex;
		align-items: center;
	}
	.commodity-goods {
		width: 654rpx;

		background-color: #fafafa;
		display: flex;
		padding-bottom: 20rpx;
		image {
			margin-top: 20rpx;
			width: 176rpx;
			height: 176rpx;
		}
		.commodity-goods-text {
			width: 420rpx;

			margin-top: 30rpx;
			margin-left: 20rpx;

			view {
				&:nth-child(1) {
					font-size: 32rpx;
					margin-top: 20rpx;
				}
				&:nth-child(2) {
					display: flex;
					justify-content: space-between;
					font-size: 27rpx;
					margin-top: 80rpx;
					text {
						&:nth-child(2) {
							color: #969799;
						}
					}
				}
			}
			.u-number-box {
				margin-top: 20rpx;
				margin-left: 200rpx;
			}
		}
	}
	.u-page {
		padding: 0;
	}

	.u-demo-block__title {
		padding: 10px 0 2px 15px;
	}

	.swipe-action {
		&__content {
			padding: 25rpx 0;

			&__text {
				font-size: 15px;
				color: $u-main-color;
				padding-left: 30rpx;
			}
		}
	}
}
// 滑动删除

// <!-- 底部编辑 -->
.bottom-cart {
	position: fixed;
	bottom: 105rpx;
	z-index: 100;
	.bottom-cart-edit {
		padding-left: 20rpx;
		width: 100%;
		height: 96rpx;
		background-color: #fff7cc;
		line-height: 96rpx;
		display: flex;
		font-size: 24rpx;
		color: #f56723;
		.u-button {
			width: 130rpx;
			height: 64rpx;
			margin: 20rpx 10rpx;
		}
	}
	.bottom-cart-purchase {
		width: 750rpx;
		height: 100rpx;
		background-color: white;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 20rpx;
		box-sizing: border-box;
		.bottom-cart-purchase1 {
			width: 270rpx;
			display: flex;
			align-items: center;
			font-size: 20rpx;
			.u-checkbox-group {
				margin-top: 12rpx;
			}
		}
		.bottom-cart-purchase2 {
			margin-right: 25rpx;
			font-size: 28rpx;
		}
		.bottom-cart-purchase3 {
			width: 220rpx;
			height: 80rpx;
		}
		.hui {
			color: red;
		}
	}
}
</style>
